const path=require('path');
let HtmlWebpackPlugin=require('html-webpack-plugin');
let MiniCssExtractPlugin=require('mini-css-extract-plugin')
let webpack=require('webpack');
const { runMain } = require('module');
let arr=['index','login','register']
let newArr=arr.map(p=>{
    return htmlobj(p)
})
function htmlobj(filename){
    return new HtmlWebpackPlugin({
        template:`./src/html/${filename}.html`,
        filename:`html/${filename}.html`,
        chunks:[`${filename}`]
    })
}
function entryList(){
    let liemt={}
    arr.forEach(p=>{
        liemt[p]=`./src/js/main/${p}.js`
    })
    return liemt
}

module.exports={
    mode:'development',//打包模式: development:开发模型，production：生成模式
    entry:{//要打包的资源
        // index:'./src/js/index.js',//参照根目录查找打包资源
        // login:'./src/js/login.js'
        ...entryList()
    },
    output:{
        path:path.resolve(__dirname,'dist'),
        filename:'js/[name].js',
        
    },
    plugins:[
       ...newArr,
        new MiniCssExtractPlugin({
            filename:'./css/[name].css'
        }),
        new webpack.ProvidePlugin({"$":"jquery"})
 ],
 module:{
     rules:[
         {
             test:/\.css$/i,
             use:[MiniCssExtractPlugin.loader,'css-loader']
         },
         {
            test:/\.scss$/i,
            use:[MiniCssExtractPlugin.loader,'css-loader','sass-loader']
        },
        {
            test:/\.(png|jpg|jpeg|gif|webm)$/,
            use:[
                {
                    loader:'url-loader',
                    options:{
                        limit:10240,
                        outputPath:'./image/',//打包后的图片存放的位置，参照dist查找
                        esModule:false//用于解决兼容问题
                    }
                }
            ]
        },
        {
            test:/\.html$/,
            use:['html-withimg-loader']
        }
     ]
 },
 devServer:{
    port:8888,//服务器端口号
    open:true,//启动服务器时自动打开一个页面
    openPage:'./html/index.html',//以dist为参照目录
    hot:true,//热部署:更新代码后自动重新打包
     proxy:{//配置代理服务器
         "/":{
             target:'http://localhost:1256'//配置后端服务器URL
         }
     }
}
}